﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using Taobao.Models;
using Taobao.Public;
using Taobao.Util;

namespace Taobao.Manger
{
    public class ScanQueueClass
    {
        //消息队列  异步
        public static void RegisterUrlQueue()
        {
            //从队列中读取url 
            ThreadPool.QueueUserWorkItem(async o =>
              {
                  while (true)
                  {
                      try
                      {
                          if (Constant.UrlQueue.Count > 0)
                          {
                              string scanModel = Constant.UrlQueue.Dequeue();

                              if (scanModel != null)
                              {
                                  //访问url操作  0 url  1 次数 2 userid
                                  var list = scanModel.Split(',').ToList();
                                  string url = list[0];//
                                  int count = Convert.ToInt32(list[1]);
                                  string userid = list[2];
                                  for (int k = 0; k < count; k++)
                                  {
                                      var code = await SpiderMethod.ScanUrl(url);
                                      if (code == "OK")
                                      {
                                          LogHelper.ScanLog("成功：" + list[0] + "," + list[1]);
                                      }
                                      else
                                      {
                                          LogHelper.ScanLog("失败：" + list[0] + "," + list[1]);
                                      }
                                      Random rd = new Random();
                                      int mins = 1000;// rd.Next(1, 5) * 60000;
                                      Thread.Sleep(mins);
                                  }

                              }
                          }
                          else
                          {
                              // 6分钟 60000
                              Thread.Sleep(6000);
                          }
                      }
                      catch (Exception ex)
                      {
                          LogHelper.ScanLog("异常：" + ex.Message);
                      }
                  }

              }, "");
        }

        public static void RegisterExceptionLogQueue()
        {
            //从队列中读取错误信息写日志
            ThreadPool.QueueUserWorkItem(o =>
            {
                while (true)
                {
                    try
                    {
                        if (Constant.ErrorQueue.Count > 0)
                        {
                            string ex = Constant.ErrorQueue.Dequeue();
                            if (ex != null)
                            {
                                //写日志  
                                LogHelper.ErrorLog(ex);
                            }
                        }
                        else
                        {
                            //
                            Thread.Sleep(60000);
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.ErrorLog(ex.Message);
                    }
                }
            }, "");
        }
    }


}